package cn.sciento.wechat.enterprise.service.impl;

import javax.annotation.Resource;
import cn.sciento.wechat.enterprise.dto.DefaultResultDTO;
import cn.sciento.wechat.enterprise.dto.UploadImageResultDTO;
import cn.sciento.wechat.enterprise.dto.UploadMediaResultDTO;
import cn.sciento.wechat.enterprise.service.WechatMaterialManageService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class WechatMaterialManageServiceImp implements WechatMaterialManageService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public UploadMediaResultDTO uploadMedia(String accessToken, String type, String name, String filename, String filelength, String contentType) {
    LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
    linkedMultiValueMap.add("name", name);
    linkedMultiValueMap.add("filename", filename);
    linkedMultiValueMap.add("filelength", filelength);
    linkedMultiValueMap.add("Content-Type", contentType);
    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity(linkedMultiValueMap, (MultiValueMap)buildHttpHeaders());
    return (UploadMediaResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=?access_token=" + accessToken + "&type=" + type, entity, UploadMediaResultDTO.class);
  }
  
  public UploadImageResultDTO uploadImage(String accessToken, String name, String filename, String contentType, String contentLength) {
    LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
    linkedMultiValueMap.add("name", name);
    linkedMultiValueMap.add("filename", filename);
    linkedMultiValueMap.add("Content-Type", contentType);
    linkedMultiValueMap.add("Content-Length", contentLength);
    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity(linkedMultiValueMap, (MultiValueMap)buildHttpHeaders());
    return (UploadImageResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + accessToken, entity, UploadImageResultDTO.class);
  }
  
  public ResponseEntity<DefaultResultDTO> getMedia(String accessToken, String media_id) {
    ResponseEntity<DefaultResultDTO> exchange = this.restTemplate.exchange("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=" + accessToken + "&media_id" + media_id, HttpMethod.GET, null, DefaultResultDTO.class);
    return exchange;
  }
  
  public ResponseEntity<DefaultResultDTO> getHDMedia(String accessToken, String media_id) {
    ResponseEntity<DefaultResultDTO> exchange = this.restTemplate.exchange("https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=" + accessToken + "&media_id" + media_id, HttpMethod.GET, null, DefaultResultDTO.class);
    return exchange;
  }
  
  protected HttpHeaders buildHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "multipart/form-data");
    return httpHeaders;
  }
}
